package com.saturn.titan.web.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.saturn.titan.web.service.ShiroDbRealm.ShiroUser;

/**
 * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求)，
 * 
 * 真正登录的POST请求由Filter完成,
 * 
 * @author lwh
 */
@Controller
public class LoginController {

	@RequestMapping(value = "/login",method = RequestMethod.GET)
	public String login() {
		//如果已登录，直接跳转至主页
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		if(user != null){
			//return "redirect:main/mainpage";
			return "index/index";
		}else{
//			Subject subject = SecurityUtils.getSubject();
//			UsernamePasswordToken token = new UsernamePasswordToken("abc","rzx@1218");
//			subject.login(token);
			return "index/login";
		}
	}


	@RequestMapping(value = "/fail",method = RequestMethod.POST)
	public String fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String userName, Model model) {
		//如果已登录，直接跳转至主页
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, userName);
		
//		Subject subject = SecurityUtils.getSubject();
//		UsernamePasswordToken token = new UsernamePasswordToken("abc","rzx@1218");
//		subject.login(token);
		return "index/index";
	}
	
	@RequestMapping(value = "/login",method = RequestMethod.POST)
	public String loginToIndex(String username,String password,Model model) {
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(username,password);
		try {
			subject.login(token);
		} catch (UnknownAccountException e) {
			model.addAttribute("message", "用户名不存在");
			return "index/login";
		}catch (IncorrectCredentialsException e) {
			model.addAttribute("message", "密码不正确");
			return "index/login";
		}
		
		return "index/index";
	}
	
	@RequestMapping(value = "/createWorkBill",method = RequestMethod.GET)
	public String createWorkBill(
			@RequestParam(value="u")String username,
			@RequestParam(value="p")String password,
			@RequestParam(value="phone")String phone,
			Model m) {
		Subject subject = SecurityUtils.getSubject();
		System.out.println(username);
		System.out.println(password);
		UsernamePasswordToken token = new UsernamePasswordToken(username,password);
		subject.login(token);
		
		m.addAttribute("toAddWorkBill", "true");
		m.addAttribute("phone",phone);
		return "index/index";
	}
	
	@RequestMapping(value = "/logout",method = RequestMethod.GET)
	public String logout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "index/index";
	}
	
	
}
